package com.wyd.jwpkglxt_ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.jwpkglxt_ssm.entity.CourseSystem;
import com.wyd.jwpkglxt_ssm.dao.CourseSystemDao;
import com.wyd.jwpkglxt_ssm.service.CourseSystemService;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * (CourseSystem)表服务实现类
 *
 * @author wyd
 * @since 2021-08-09 23:06:34
 */
@Service("courseSystemService")
public class CourseSystemServiceImpl implements CourseSystemService {

    @Value("${baseDir}")
    private String baseDir;

    @Autowired
    private CourseSystemDao courseSystemDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CourseSystem queryById(Integer id) {
        return this.courseSystemDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<CourseSystem> queryAllByLimit(int offset, int limit) {
        return this.courseSystemDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param courseSystem 实例对象
     * @return 实例对象
     */
    @Override
    public CourseSystem insert(CourseSystem courseSystem) {
        this.courseSystemDao.insert(courseSystem.setCreateTime(new Timestamp(System.currentTimeMillis())));
        return courseSystem;
    }

    /**
     * 修改数据
     *
     * @param courseSystem 实例对象
     * @return 实例对象
     */
    @Override
    public CourseSystem update(CourseSystem courseSystem) {
        this.courseSystemDao.update(courseSystem.setUpdateTime(new Timestamp(System.currentTimeMillis())));
        return this.queryById(courseSystem.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        if(id!=null){
            CourseSystem courseSystem = this.courseSystemDao.queryById(id);
            File file = new File(baseDir+"/"+courseSystem.getFileName()+courseSystem.getExtensionName());
            if(file.delete()){
                return this.courseSystemDao.deleteById(id) > 0;
            }
        }
        return false;
    }

    @Override
    public PageInfo<CourseSystem> queryAllByPage(Integer pageNum, Integer pageSize, CourseSystem courseSystem) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseSystem> courseSystems = this.courseSystemDao.queryAll(courseSystem);
        return new PageInfo<>(courseSystems);
    }

    @Override
    public Boolean deleteCourseSystemBatch(List<Integer> courseSystemIds) {
        if(!courseSystemIds.isEmpty()){
            List<CourseSystem> courseSystems = this.courseSystemDao.queryAllByIdBatch(courseSystemIds);
            for(int i = 0;i<courseSystems.size();i++){
                String fileName = courseSystems.get(i).getFileName();
                String extensionName = courseSystems.get(i).getExtensionName();
                File file = new File(baseDir+"/"+fileName+extensionName);
                if(file.delete()){
                    this.courseSystemDao.deleteById(courseSystems.get(i).getId());
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public List<String> queryAllExtensionName() {
        return this.courseSystemDao.queryAllExtensionName();
    }
}